import { createRouter, createWebHashHistory } from 'vue-router'

import Layout from '@/layout/index.vue'
// import Layout from './main'

export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    hidden: true
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/error/403.vue'),
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/index.vue'),
        meta: {
          title: '首页看板',
          icon: 'home'
        }
      }
    ],
    meta: {
      keepAlive: false
    }
  },
  // 菜谱管理
  {
    path: '/recipe2',
    component: Layout,
    redirect: '/recipe',
    children: [
      {
        path: '/recipe',
        name: 'recipe',
        component: () => import('@/views/recipe/index.vue'),
        meta: {
          title: 'recipeManage',
          icon: 'recipe'
        }
      }
    ],
    meta: {
      keepAlive: false
    }
  },
  // 设备管理
  {
    path: '/device',
    component: Layout,
    redirect: '/device/Manage',
    children: [
      {
        path: '/device/Manage',
        name: 'deviceManage',
        component: () => import('@/views/device/device/index.vue'),
        meta: {
          title: 'deviceManage'
        }
      },
      {
        path: '/device/peripheral',
        name: 'peripheral',
        component: () => import('@/views/device/peripheral/index.vue'),
        meta: {
          title: '外设管理'
        }
      },
      {
        path: '/device/placement',
        name: 'placement',
        component: () => import('@/views/device/placement/index.vue'),
        meta: {
          title: '投放管理'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: 'deviceManage',
      icon: 'device'
    }
  },
  // 食料管理
  {
    path: '/foodstuff',
    component: Layout,
    redirect: '/foodstuff/ingredients',
    children: [
      {
        path: '/foodstuff/ingredients',
        name: 'ingredients',
        component: () => import('@/views/foodstuff/ingredients/index.vue'),
        meta: {
          title: '食材管理'
        }
      },
      {
        path: '/foodstuff/seasoning',
        name: 'seasoning',
        component: () => import('@/views/foodstuff/seasoning/index.vue'),
        meta: {
          title: '调料管理'
        }
      },
      {
        path: '/foodstuff/process',
        name: 'process',
        component: () => import('@/views/foodstuff/process/index.vue'),
        meta: {
          title: '处理工艺'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: '食料管理',
      icon: 'foodstuff'
    }
  },
  // 统计分析
  {
    path: '/statistics',
    component: Layout,
    redirect: '/statistics/analyze',
    children: [
      {
        path: '/statistics/analyze',
        name: 'statisticsAnalyze',
        component: () => import('@/views/statistics/index.vue'),
        meta: {
          title: '统计分析',
          icon: 'device'
        }
      }
    ],
    meta: {
      keepAlive: false
    }
  },
  // 用户中心
  {
    path: '/userCenter',
    name: 'userCenter',
    component: Layout,
    redirect: '/userCenter/organize',
    alwaysShow: true,
    children: [
      // {
      //   path: '/userCenter/organize',
      //   name: 'organize',
      //   component: () => import('@/views/userCenter/organize/index.vue'),
      //   hidden: true,
      //   meta: {
      //     title: 'organizeManage'
      //   }
      // },
      {
        path: '/userCenter/department',
        name: 'department',
        component: () => import('@/views/userCenter/department/index.vue'),
        meta: {
          title: 'deptManage'
        }
      },
      {
        path: '/userCenter/user',
        name: 'user',
        component: () => import('@/views/userCenter/user/index.vue'),
        meta: {
          title: 'userManage'
        }
      },
      {
        path: '/userCenter/role',
        name: 'role',
        component: () => import('@/views/userCenter/role/index.vue'),
        meta: {
          title: 'roleManage'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: 'userCenter',
      icon: 'userCenter'
    }
  }
]

// 所有需要动态控制的路由定义，会在权限模块根据后台返回的权限信息过滤
export const asyncRoutes = []

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 })
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  g_router && g_router.clearRoutes()
  // const newRouter = createRouter({
  //   history: createWebHashHistory(),
  //   routes: constantRoutes,
  //   // 刷新时，滚动条位置还原
  //   scrollBehavior: () => ({ left: 0, top: 0 })
  // })
  // router.matcher = newRouter.matcher // reset router
  constantRoutes.forEach(item => {
    g_router && g_router.addRoute(item)
  })
}

// 点击菜单时，捕捉报错，刷新页面
router.onError(error => {
  // if (error.message.includes('Failed to fetch dynamically imported module')) {
  //   ElMessage({
  //     message: g_t('route.routerUpgradeTips'),
  //     type: 'warning',
  //     duration: 1500,
  //     offset: 60
  //   })
  //   // reload() 类似于点击浏览器上的刷新页面按钮
  //   location.reload()
  // }
})

export default router
